#include "main.h" // include the main header file


//struct CBoard::BoardVertex // declaration of the structure that holds the four vertices
//{	
//    D3DXVECTOR3 Location;   // vector that specifies the location of each vertex
//    D3DXVECTOR2 TextureCoordinate;  // 2D vector that specifies the coordinates of the textures
//};
// initialize the pointers
// class constructor
CBoard::CBoard(void)
{
	p_d3d10TextureResourceView = NULL;
	p_d3dEffect = NULL;
	p_d3dEffectProjectMatrix = NULL;
	p_d3dEffectShaderResource = NULL;
	p_d3dEffectTech = NULL;
	p_d3dEffectViewMatrix = NULL;
	p_d3dEffectWorldMatrix = NULL;
	//p_indexBuffer = NULL;
	//p_vertexBuffer = NULL;
	p_inputLayout = NULL;
	run_once = 0;
};
// class destructor
CBoard::~CBoard(void)
{
};
// setup the board drawing process and the texture to go with it
HRESULT CBoard::setupBoard()
{
	HRESULT hresult_;

	// load the shader effect file
	hresult_ = D3DX10CreateEffectFromFile(
					L"effect.fx", NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, 
                    direct3D.getDevice(), NULL, NULL, &p_d3dEffect, NULL, NULL);
	
	// if the file is missing stop the program
	if(FAILED(hresult_))
	{
		return hresult_;
	}

	// setup the effect technique and load it from the shader file
	p_d3dEffectTech = p_d3dEffect->GetTechniqueByName("TextureEffect");
   
	// get the matrix effect variables from the shader file
    p_d3dEffectWorldMatrix = p_d3dEffect->GetVariableByName("Worldmatrix")->AsMatrix();
    p_d3dEffectViewMatrix = p_d3dEffect->GetVariableByName("Viewmatrix")->AsMatrix();
    p_d3dEffectProjectMatrix = p_d3dEffect->GetVariableByName("Projectionmatrix")->AsMatrix();
	p_d3dEffectShaderResource = p_d3dEffect->GetVariableByName("texture_")->AsShaderResource();
	p_inputEffectShaderResource = p_d3dEffect->GetVariableByName("texture_")->AsShaderResource();
	// define the input layout
    D3D10_INPUT_ELEMENT_DESC inputlayout[] =
    {
        {   // get the position of the vertices
			"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, 
			D3D10_INPUT_PER_VERTEX_DATA, 0 
		},
		
        { // get the texture coordinates
			"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, 
			D3D10_INPUT_PER_VERTEX_DATA, 0 
		}, 
    };
    
	int numberOfElements = sizeof(inputlayout) / sizeof(inputlayout[0]);
  
    // specify the input layout object and create it 
	D3D10_PASS_DESC PassDescription;
	p_d3dEffectTech->GetPassByIndex(0)->GetDesc(&PassDescription);

	hresult_ = direct3D.getDevice()->CreateInputLayout(
								inputlayout, numberOfElements,
								PassDescription.pIAInputSignature,
								PassDescription.IAInputSignatureSize,
								&p_inputLayout);

	// if the input layout doesn't create stop the program
	if(FAILED(hresult_))
		return hresult_;


	// bind the input layout to the Input Assembler stage
	direct3D.getDevice()->IASetInputLayout(p_inputLayout);

		// specify the four vertices of the board
		vertex_data[0].set_xyz(-0.9f, -0.9f, 0.0f); // bottom left
		vertex_data[0].setTex_xy(0.0f, 0.0f); 
		vertex_data[1].set_xyz(0.9f, -0.9f, 0.0f); // bottom right
		vertex_data[1].setTex_xy(1.0f, 0.0); 
		vertex_data[2].set_xyz(0.9f, 0.9f, 0.0f); // top right
		vertex_data[2].setTex_xy(1.0f, 1.0f); 
		vertex_data[3].set_xyz(-0.9f, 0.9f, 0.0f); // top left
		vertex_data[3].setTex_xy(0.0f, 1.0f); 
		vertex_data[4].set_xyz(0.6f, 0.6f, -0.5f); // top right
		vertex_data[4].setTex_xy(1.0f, 1.0f);
		vertex_data[5].set_xyz(0.6f, 0.3, -0.5f); // bottom right
		vertex_data[5].setTex_xy(1.0f, 0.0f);
		vertex_data[6].set_xyz(0.3f, 0.3f, -0.5f); // bottom left
		vertex_data[6].setTex_xy(0.0f, 0.0f);
		vertex_data[7].set_xyz(0.3f, 0.6f, -0.5f); // top left
		vertex_data[7].setTex_xy(0.0f, 1.0f);


		index_data[0] = 3;
		index_data[1] = 1;
		index_data[2] = 0;
		index_data[3] = 2;
		index_data[4] = 1;
		index_data[5] = 3;	
		index_data[6] = 3;
		index_data[7] = 1;
		index_data[8] = 2;
		index_data[9] = 0;
		index_data[10] = 1;
		index_data[11] = 3;

	// load the texture
	hresult_ = D3DX10CreateShaderResourceViewFromFile(direct3D.getDevice(), L"board_texture.bmp", NULL, NULL, &p_d3d10TextureResourceView, NULL ); 
    // if the texture doesn't load interrupt the program
	if(FAILED(hresult_))
        return hresult_;

	// set the texture

	// identify the world matrix
	D3DXMatrixIdentity(&g_WorldMatrix);

	// set the view matrix using a vector for the eye coordinate, look at and updirection of the view
	D3DXVECTOR3 EyeCoord( 0.0f, 0.0f, -2.0f);
	D3DXVECTOR3 LookAt(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 UpDir(0.0f, 3.0f, 0.0f);
	D3DXMatrixLookAtLH(&g_ViewMatrix, &EyeCoord, &LookAt, &UpDir);

 	
	//set the perspective field of view matrix
	D3DXMatrixPerspectiveFovLH(&g_ProjectionMatrix, (float)D3DX_PI*0.25f, 800/600, 0.1f, 100.0f);

	// if everything is executed right return true
	return S_OK;
};

// define the draw function
void CBoard::draw()
{
	if(run_once == 0)
	{
		p_d3dEffectViewMatrix->SetMatrix((float*)&g_ViewMatrix); // set the view matrix and bind it to the variable
		p_d3dEffectProjectMatrix->SetMatrix((float*)&g_ProjectionMatrix); // set the projection matrix and bind it to the variable
		p_d3dEffectShaderResource->SetResource(p_d3d10TextureResourceView); // set the texture resource variable
		
	}

	// start rendering by declaring the backbuffer colour and clearing the render target view with the given colour	
	float ClearBufferColour[4] = {1.0f, 1.0f, 1.0f, 1.0f};
	direct3D.getDevice()->ClearRenderTargetView(direct3D.getTargetView(), ClearBufferColour);
	
	// update the world matrix
    p_d3dEffectWorldMatrix->SetMatrix((float*)&g_WorldMatrix);
	
	// obtain the technique variable for the effect technique
	D3D10_TECHNIQUE_DESC technique;
	p_d3dEffectTech->GetDesc(&technique);

	// render by looping over the technique passes
	for(UINT i = 0; i < technique.Passes; ++i)
	{
		p_d3dEffectTech->GetPassByIndex(i)->Apply(0);
		direct3D.getDevice()->DrawIndexed(6, 0, 0);
			
	}
	drawNew();
}
void CBoard::drawNew()
{
	if (run_once == 0)
	{
		D3DX10CreateShaderResourceViewFromFile(direct3D.getDevice(), L"X.bmp", NULL, NULL, &p_inputTextureResource, NULL);
		p_inputEffectShaderResource->SetResource(p_inputTextureResource);
		
	}
	D3D10_TECHNIQUE_DESC technique;
	p_d3dEffectTech->GetDesc(&technique);

	// render by looping over the technique passes
	for(UINT i = 0; i < technique.Passes; ++i)
	{
		p_d3dEffectTech->GetPassByIndex(i)->Apply(0);
		direct3D.getDevice()->DrawIndexed(6, 6, 4);	
	}
		run_once = 1; // variables are set once they don't need to be set another time
}